/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.kmcsoft.goldshop.goldexchange;

import com.kmcsoft.core.ui.GenericController;
import com.kmcsoft.goldshop.generic.service.ShopGenericService;
import com.kmcsoft.goldshop.goldexchange.service.GoldExchangeService;
import com.kmcsoft.goldshop.model.exchange.GoldExchange;
import com.kmcsoft.goldshop.model.exchange.GoldExchangeDetail;
import com.kmcsoft.goldshop.model.master.ProductGold;
import com.kmcsoft.util.Utilities;
import com.kmcsoft.zkex.PagingListModel;
import java.util.Collection;
import java.util.HashMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.zkoss.zk.ui.Component;

/**
 *
 * @author kmcsoft02
 */
public class GoldExchangeComposer extends GenericController<GoldExchange, GoldExchangeService> {

    private String basicCurrency;
    @Autowired
    ShopGenericService shopGenericService;

    //<editor-fold defaultstate="collapsed" desc="doAfterCompose">
    @Override
    public void doAfterCompose(Component comp) throws Exception {
        super.doAfterCompose(comp);
        basicCurrency = shopGenericService.getBasicCurrency().getCode();
    }
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc="PagingListModel">
    @Override
    public PagingListModel getListModel() {
        String strFilter = "createdBy.name.matches('(?i).*" + filter.getValue().trim() + ".*')";
        PagingListModel<GoldExchange> model = Utilities.getPagingListModel(objectClass, HashMap.class, "id,createdBy.name,createdDate", strFilter);
        model.setOrdering("createdDate descending");
        model.initialize();
        return model;
    }

    public void setSelected(HashMap selected) {
        super.selected = service.getGoldExchangeById(selected.get("id"));
    }
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc="onCreate">
    public void onCreate() {
        onClick$new(null);
    }
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc="doAfterNew">
    @Override
    protected void doAfterNew() {
        super.doAfterNew();
        Collection<ProductGold> productGolds = service.getProductGolds();
        for (ProductGold productGold : productGolds) {
            selected.getDetails().add(new GoldExchangeDetail(productGold));
        }
    }
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc="doBeforeSave">
    @Override
    protected void doBeforeSave() {
        super.doBeforeSave();
        for (GoldExchangeDetail detail : selected.getDetails()) {
//            detail=service.getGoldExchangeDetailById(detail.getId());
            detail.getProductGold().setBuyingPrice(detail.getBuyingPrice());
            detail.getProductGold().setSellingPrice(detail.getSellingPrice());
        }
    }
    //</editor-fold>

    @Override
    protected void doAfterSave() {
        super.doAfterSave();
        super.setSelected(service.getGoldExchangeById(selected.getId()));
    }
    
    

    public String getBasicCurrency() {
        return basicCurrency;
    }
}
